package com.cartoonrabbit.config.security.service;


import com.cartoonrabbit.config.security.pojo.LoginUser;
import com.cartoonrabbit.config.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.regex.Pattern;


@Service("ss")
public class PermissionService
{

    /**
     * 判断用户是否拥有某个角色
     * 
     * @param role 角色字符串
     * @return 用户是否具备某角色
     */
    public boolean hasRole(String role)
    {
        if (ObjectUtils.isEmpty(role))
        {
            return false;
        }
        // 获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtils.isEmpty(loginUser) || ObjectUtils.isEmpty(loginUser.getUser().getRole()))
        {
            return false;
        }


            String roleName = loginUser.getUser().getRole();
//            roleName.equals(role)
            if (Pattern.matches("^"+role+".*$",roleName))
            {
                return true;
            }
        return false;
    }

    /**
     * 允许所有角色访问
     * @return 用户是否具备某角色
     */
    public boolean permitAll()
    {
        return true;
    }

}
